//
//  WBEmoticonPageView.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/18.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit

class WBEmoticonPageView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }
    
    // MARK: - 设置UI
    func setupUI() {
        
        //注册cell
        registerClass(WBEmoticonPageViewCell.self, forCellWithReuseIdentifier: "item")
        //设置数据源
        dataSource = self
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置Layout
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        //设置item大小
        layout.itemSize = self.frame.size
        
        //设置间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        //设置方向
        layout.scrollDirection = .Horizontal
        
    }
}

extension WBEmoticonPageView:UICollectionViewDataSource{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return WBEmoticonTool.sharedEmoticonTool.allEmoticons.count
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WBEmoticonTool.sharedEmoticonTool.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("item", forIndexPath: indexPath) as! WBEmoticonPageViewCell
        cell.backgroundColor = self.backgroundColor
  
        cell.emoticons = WBEmoticonTool.sharedEmoticonTool.allEmoticons[indexPath.section][indexPath.item]
        return cell
    }

}
